package artid.com.arttogoa.adapter;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import artid.com.arttogoa.AddAddressActivity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.AddressManagementParser;
import artid.com.arttogoa.parser.OneStateParser;
import artid.com.arttogoa.util.AlertUtils;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：AddressManagement_Adapter   
 * 类描述   ：地址管理适配器   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-18 下午5:39:53   
 *
 */
public class AddressManagement_Adapter extends BaseAdapter {
	private Context mContext;
	private List<AddressManagementParser> list;
	private Boolean isAddressManagementInetnt;//是否为“地址管理”跳转过来的(默认false)

	public AddressManagement_Adapter(Context mContext,
			List<AddressManagementParser> list,
			Boolean isAddressManagementInetnt) {
		super();
		this.mContext = mContext;
		this.list = list;
		this.isAddressManagementInetnt = isAddressManagementInetnt;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_addressmanagement, null);
			holder.name=(TextView) v.findViewById(R.id.item_addressmanagement_name);
			holder.phone=(TextView) v.findViewById(R.id.item_addressmanagement_phone);
			holder.address=(TextView) v.findViewById(R.id.item_addressmanagement_address);
			holder.isDefault=(TextView) v.findViewById(R.id.item_addressmanagement_isDefault);
			holder.delete=(TextView) v.findViewById(R.id.item_addressmanagement_delete);
			holder.edite=(TextView) v.findViewById(R.id.item_addressmanagement_edite);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		if(list.size()==1){
			//只有一条地址的时候将删除按钮隐藏
			holder.delete.setVisibility(View.GONE);
		}
		holder.name.setText(list.get(position).getRealname());
		holder.phone.setText(list.get(position).getMob_phone());
		holder.address.setText(list.get(position).getAddressinfo());
		if("1".equals(list.get(position).getIs_default())){
			//默认
			Drawable drawable = mContext.getResources().getDrawable(R.drawable.paiz_information_select_sel2);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
			holder.isDefault.setCompoundDrawables(drawable,null,null, null);//画在左边
		}else{
			//非默认
			Drawable drawable = mContext.getResources().getDrawable(R.drawable.paiz_information_select_nor);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
			holder.isDefault.setCompoundDrawables(drawable,null,null, null);//画在左边
		}
		holder.isDefault.setCompoundDrawablePadding(4);//设置图片和text之间的间距  
		//设置默认地址
		holder.isDefault.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setDefaultAddress(list.get(position).getAddress_id(), position);
			}
		});
		//删除地址
		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDeleteDialog(position);
			}
		});
		//编辑地址
		holder.edite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AddressManagementParser mAddressManagementParser=new AddressManagementParser();
				mAddressManagementParser=list.get(position);
				Intent i=new Intent(mContext,AddAddressActivity.class);
				i.putExtra("isEditAddress", true);
				i.putExtra("isAddressManagementInetnt", isAddressManagementInetnt);
				i.putExtra("AddressManagementParser", mAddressManagementParser);
				((Activity) mContext).startActivityForResult(i, 1);
			}
		});
		return v;
	}
	class ViewHolder {
		TextView name,phone,address,isDefault,delete,edite;
	}

	/**
	 * 设置默认地址
	 * is_default	0不是，1是
	 */
	private void setDefaultAddress(String address_id,final int position){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.defaultAddress,
				VolleyMap.getInstance().defaultAddress(address_id,"1"),
				OneStateParser.class, new VolleyCallback() {
			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				OneStateParser bean=(OneStateParser) vd.getData();
				if("1".equals(bean.getState())){
					//设置默认地址成功
					for (int i = 0; i < list.size(); i++) {
						list.get(i).setIs_default("0");
					}
					list.get(position).setIs_default("1");
					notifyDataSetChanged();
				}
			}
			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "网络开小差了，请重新点击设置");
			}
		});
	}

	/**
	 * 删除对话框
	 * @param position
	 */
	private void showDeleteDialog(final int position){
		AlertUtils.AlertTwoBtnDialog(mContext, "是否要删除该条地址信息?", "确定", "取消", Integer.parseInt(NetUtils
				.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH)), new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.tv_ok:
					//确定按钮
					AlertUtils.dismiss();
					deleteAddress(list.get(position).getAddress_id(), position);
					break;
				case R.id.tv_no:
					//取消按钮
					AlertUtils.dismiss();
					break;

				default:
					break;
				}
			}
		});
	}

	/**
	 * 删除地址
	 */
	private void deleteAddress(String address_id,final int position){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.deleteAddress,
				VolleyMap.getInstance().deleteAddress(address_id),
				OneStateParser.class, new VolleyCallback() {
			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				OneStateParser bean=(OneStateParser) vd.getData();
				if("1".equals(bean.getState())){
					//删除地址成功
					Intent intent0 = new Intent("com.AddressManagement_Activity.receiver");  
					intent0.putExtra("refreshData", true);
					mContext.sendBroadcast(intent0);
					if(list.size()==0){
						//如果没有地址之后通知地址列表显示没有数据的样子
						Intent intent = new Intent("com.AddressManagement_Activity.receiver");  
						intent.putExtra("noAddress", true);
						mContext.sendBroadcast(intent);
						if(!isAddressManagementInetnt){
							//如果是从选择地址操作过程中，地址一条都没有了通知将数据清空
							Intent intent2 = new Intent("com.MyDeliverGoods_Activity.receiver");
							intent2.putExtra("addressNull", true);
							mContext.sendBroadcast(intent2);
						}
					}
				}
			}
			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "网络开小差了，请重新点击设置");
			}
		});
	}

}
